package com.pg.agent.manager.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 接口的应用服务
 *
 * @author 胡华平
 * @date 2023-08-22 14:30:25
 */
@Slf4j
@RestController
@RequestMapping("interface/")
public class InterfaceController {

  /*  @Resource
    private InterfaceService interfaceService;


    @ApiOperation(value = "接口分页查询", httpMethod = "POST")
    @PostMapping("page")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public IPage<InterfaceVO> page(@RequestBody InterfaceQuery condition) {

        LoginVO loginUser = LoginUtils.getLoginUser();
        condition.setBelongId(loginUser.getBelongId());
        return interfaceService.page(condition);
    }

    @ApiOperation(value = "接口分页查询", httpMethod = "GET")
    @GetMapping("page")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public IPage<InterfaceVO> pages(InterfaceQuery condition) {

        LoginVO loginUser = LoginUtils.getLoginUser();
        condition.setBelongId(loginUser.getBelongId());
        return interfaceService.page(condition);
    }

    @ApiOperation(value = "接口详情", httpMethod = "GET")
    @GetMapping("/get")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public InterfaceDetailVO get(Long id, HttpServletRequest request) {
        log.info("receive Interface get param=[id={}]", JSON.toJSONString(id));
        InterfaceDetailVO interfaceDetailVO = interfaceService.get(id);

        // 对API接口单独处理请求方式和路径
        if (interfaceDetailVO.getInterfaceType().equals(0)) {
            interfaceDetailVO.setRequestMethod("POST");
            interfaceDetailVO.setUrl(StringUtils.nullToEmpty(ClifeUtil.getAllDomain(request)) + "/v4/web/decision-engine/interface/call");
        }

        return interfaceDetailVO;
    }

    @ApiOperation(value = "接口详情", httpMethod = "GET")
    @GetMapping("/ignore/getById")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public InterfaceDetailVO getById(Long id) {
        log.info("receive Interface get param=[id={}]", JSON.toJSONString(id));
        InterfaceDetailVO interfaceDetailVO = interfaceService.get(id);
        return interfaceDetailVO;
    }

    @ApiOperation(value = "项目接口列表", httpMethod = "GET")
    @GetMapping("/listByProjectId")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public MultiResponse<InterfaceVO> listByProjectId() {
        log.info("receive Interface listByProjectId ");
        return interfaceService.listByProjectId();
    }

    @ApiOperation(value = "接口列表", httpMethod = "GET")
    @GetMapping("/list")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public List<InterfaceVO> list(InterfaceQuery interfaceQuery) {
        log.info("receive Interface listByProjectId param=[{}]", JSON.toJSONString(interfaceQuery));
        return interfaceService.list(interfaceQuery);
    }

    @ApiOperation(value = "接口新增", httpMethod = "POST")
    @PostMapping("/add")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Long add(@RequestBody InterfaceCreateCmd interfaces) {
        log.info("receive Interface add param=[{}]", JSON.toJSONString(interfaces));
        if (interfaces.getOutputStreamType() == null) {
            interfaces.setOutputStreamType(0);
        }
        return interfaceService.add(interfaces);
    }

    @ApiOperation(value = "接口编辑", httpMethod = "POST")
    @PostMapping("/update")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Long update(@RequestBody InterfaceCreateCmd interfaces) {
        log.info("receive Interface update param=[id={}]", JSON.toJSONString(interfaces));
        return interfaceService.update(interfaces);
    }

    @ApiOperation(value = "接口删除", httpMethod = "POST")
    @PostMapping("/delete")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Boolean delete(@RequestBody DeleteCmd deleteCmd) {
        log.info("receive Interface delete param=[{}]", JSON.toJSONString(deleteCmd));
        return interfaceService.delete(deleteCmd.getId());
    }

    @ApiOperation(value = "复制接口", httpMethod = "POST")
    @PostMapping("/addCopy")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Long addCopy(@RequestBody InterfaceDTO interfaceDTO) {
        log.info("receive Interface addCopy param=[{}]", JSON.toJSONString(interfaceDTO));
        return interfaceService.addCopy(interfaceDTO);
    }


    @ApiOperation(value = "接口启用", httpMethod = "GET")
    @GetMapping("/enable")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Long enable(Long id) {
        log.info("receive Interface enable param=[id={}]", id);
        return interfaceService.updateStatus(id, CommonConstant.ENABLE);
    }


    @ApiOperation(value = "接口禁用", httpMethod = "GET")
    @GetMapping("/disable")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Long disable(Long id) {
        log.info("receive Interface disable param=[id={}]", id);
        return interfaceService.updateStatus(id, CommonConstant.DISABLE);
    }

    @ApiOperation(value = "接口调试", httpMethod = "POST")
    @PostMapping("/testInterface")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Object testInterface(@RequestBody TestInterfaceCmd testInterfaceCmd) {
        log.info("receive Interface testInterface param=[{}],ip=[{}]", JSON.toJSONString(testInterfaceCmd), ShiroUtils.getIp());
        return interfaceService.testInterface(testInterfaceCmd);
    }

    @ApiOperation(value = "接口流式调试", httpMethod = "POST")
    @PostMapping("/testInterfaceStream")
    public void testInterfaceStream(@RequestBody TestInterfaceCmd testInterfaceCmd, HttpServletResponse response) {
        log.info("receive Interface testInterface param=[{}],ip=[{}]", JSON.toJSONString(testInterfaceCmd), ShiroUtils.getIp());
        interfaceService.testInterfaceStream(testInterfaceCmd, response);
    }


    @ApiOperation(value = "决策引擎调用", httpMethod = "POST")
    @PostMapping("/call")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public Object call(@RequestBody Map<String, Object> inParam, HttpServletRequest request) {
        ClifeAssert.paramFail(ObjectUtil.isEmpty(request.getHeader("interfaceId")), "interfaceId不能为空");
        inParam.put("interfaceId", request.getHeader("interfaceId"));
        log.info("receive Interface testInterface param=[{}]", JSON.toJSONString(inParam));
        return interfaceService.call(inParam);
    }


    @ApiOperation(value = "查询第三方接口列表", httpMethod = "GET")
    @GetMapping("/listThird")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public List<InterfaceVO> listThird() {
        log.info("receive Interface listThird");
        return interfaceService.listThird();
    }

    @ApiOperation(value = "更新一下历史数据入参", httpMethod = "GET")
    @GetMapping("/updateBatch")
    @AutoTraceLog(type = ServiceType.SPRINGMVC, printParams = true, printReturn = true)
    public void updateBatch() {
        log.info("receive Interface updateBatch param=[{}]");
        interfaceService.updateBatch();
    }
*/
}
